package jiang.study.dadongserver;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class MainClient {
    /**
     * TimeOut*Pers就是超时时间 毫秒
     */
    static final int TimeOut = 20;
    static final int Pers = 1000;
    static final String Host = "127.0.0.1";
    static final int Port = 12345;

    public void startClient() {
        try {
            System.out.println("进入client");
            MyClientRunable myClientRunable = new MyClientRunable();
            new Thread(myClientRunable).start();

            Thread.sleep(3000);
            JiangData data = new JiangData();
            data.name = "jaingzhongyi";
            myClientRunable.getDataWait(data);
            System.out.println("client-send");
            data = new JiangData();
            data.name = "yizongjiang";
            data.type = 2;
            Thread.sleep(2000);
            myClientRunable.getDataWait(data);
            System.out.println("client-send");
            data = new JiangData();
            data.type = 3;
            data.name = "1231231231";
            Thread.sleep(2000);
            myClientRunable.getDataWait(data);
            System.out.println("client-send");
//            System.out.println("进入client2");
//            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static class MyClientRunable implements Runnable {
        ObjectOutputStream objectOutputStream = null;
        ObjectInputStream objectInputStream = null;
        InputStream input = null;
        Boolean isUse = false;
        JiangData beatData = new JiangData("test");
        Socket socket = null;
        private int timeCount = 0;

        private synchronized boolean getIsUseSetTrue() {
            if (isUse) {
                return true;
            }
            isUse = true;
            return false;
        }

        private synchronized void notifyUse() {
            isUse = false;
            notify();
        }

        private synchronized void waitUse() {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }

        @Override
        public void run() {
            try {
                getIsUseSetTrue();
                System.out.println("client-run1");
                socket = new Socket(Host, Port);
                socket.setTcpNoDelay(true);
                System.out.println("client-run2");
                input = socket.getInputStream();
                System.out.println("client-run3");
                objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                System.out.println("client-run4-");
                objectOutputStream.writeObject(beatData);
                objectOutputStream.flush();
                System.out.println("client-run4");
                objectInputStream = new ObjectInputStream(input);
                objectInputStream.readObject();
                System.out.println("client-run5");
                timeCount = 0;
                notifyUse();
                while (timeCount < TimeOut) {
                    timeCount++;
                    if (timeCount >= TimeOut) {
                        if (getIsUseSetTrue()) continue;
                        getData(beatData);
                        notifyUse();
                    } else {
                        Thread.sleep(Pers);
                    }
                }
//                Thread.sleep(30000);
                System.out.println("client-runable-end");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close();
            }
        }

        private JiangData getData(JiangData data) {
            JiangData resultData = null;
            try {
                timeCount = 0;
                objectOutputStream.writeObject(data);
                objectOutputStream.flush();
                int i = 0;
                while (input.available() < 1 && i < 10) {
                    i++;
                    System.out.println("wait-" + i);
                    Thread.sleep(500);
                }
                resultData = (JiangData) objectInputStream.readObject();
                System.out.println(resultData.type + "=" + resultData.name);
            }catch (SocketException se){

            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultData;
        }

        private JiangData getDataWait(JiangData data) {
            JiangData resultData = null;
            while (getIsUseSetTrue()) waitUse();
            resultData = getData(data);
            notifyUse();
            return resultData;
        }

        void close() {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                objectOutputStream = null;
            }
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                objectInputStream = null;
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                socket = null;
            }
        }
    }
}
